package org.xxpay.manage.common.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.entity.SysAreaCode;
import org.xxpay.core.entity.SysCityCode;
import org.xxpay.core.entity.SysProvinceCode;
import org.xxpay.manage.common.ctrl.BaseController;

import java.util.List;

@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/area_code")
public class AreaCodeController extends BaseController {

    /** 查询省 **/
    @RequestMapping("/provinces")
    public XxPayResponse provinceList() {
        SysProvinceCode sysProvinceCode = new SysProvinceCode();
        List<SysProvinceCode> list = commonService.sysProvinceCodeService.select(sysProvinceCode);
        return XxPayResponse.buildSuccess(list);
    }

    /** 根据省code, 查询下属市 **/
    @RequestMapping("/cities")
    public XxPayResponse cityList() {
        JSONObject param = getJsonParam(request);
        SysCityCode sysCityCode = getObject(param, SysCityCode.class);
        Integer provinceCode = getIntegerRequired(param, "provinceCode");
        sysCityCode.setProvinceCode(provinceCode);
        List<SysCityCode> list = commonService.sysCityCodeService.select(sysCityCode);
        return XxPayResponse.buildSuccess(list);
    }

    /** 根据市code, 查询下属 地区/县 **/
    @RequestMapping("/areas")
    public XxPayResponse areaList() {
        JSONObject param = getJsonParam(request);
        SysAreaCode sysAreaCode = getObject(param, SysAreaCode.class);
        Integer cityCode = getIntegerRequired(param, "cityCode");
        sysAreaCode.setCityCode(cityCode);
        List<SysAreaCode> list = commonService.sysAreaCodeService.select(sysAreaCode);
        return XxPayResponse.buildSuccess(list);
    }


    /** 根据地区详细信息 **/
    @RequestMapping("/get")
    public XxPayResponse getAreaInfo() {
        JSONObject param = getJsonParam(request);
        Integer areaCode = getIntegerRequired(param, "areaCode");
        return XxPayResponse.buildSuccess(commonService.sysAreaCodeService.find(areaCode));
    }

}